﻿using Define;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tools.Base;
using Tools.EventObjectMoudle;

namespace LogicCenter.Logic
{
    public class PlayerManager : SingletonTemplate<PlayerManager>
    {
        /// <summary>
        /// key - PlayerID value - PlayerName
        /// </summary>
        private Dictionary<int, GameClient> m_dicPlayerInfo = new Dictionary<int, GameClient>();

        public void InsertPlayer(int nPlayerID, string strName)
        {
            lock(this)
            {
                m_dicPlayerInfo[nPlayerID] = new GameClient()
                {
                    strName = strName,
                    nRoleID = nPlayerID,
                };

                GlobalEventSource.getInstance().fireEvent((int)EventTypes.PlayerInit, m_dicPlayerInfo[nPlayerID]);
            }
        }

        public int GetPlayerCount()
        {
            lock(this)
            {
                return m_dicPlayerInfo.Count;
            }            
        }

        DateTime tLast_Tick = DateTime.Now;
        public void Tick()
        {
            DateTime tNow = DateTime.Now;
            if ((tNow - tLast_Tick).TotalMilliseconds < 10)
                return;

            tLast_Tick = tNow;

            lock(this)
            {
                foreach(var it in m_dicPlayerInfo)
                {
                    if (it.Value != null)
                        it.Value.HeartBeat();
                }
            }
        }
    }
}
